/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef GY_DIAGRAMSCENE_H
#define GY_DIAGRAMSCENE_H
#include <QGraphicsScene>
#include "GY_DiagramItem.h"
#include "GY_DiagramTextItem.h"
#include "GY_PortItem.h"
#include "GY_Arrow.h"
#include "GY_ControlLibrary_global.h"
QT_BEGIN_NAMESPACE
class QGraphicsSceneMouseEvent;
class QMenu;
class QPointF;
class QGraphicsLineItem;
class QFont;
class QGraphicsTextItem;
class QColor;
QT_END_NAMESPACE

/**
 * @brief 蓝图场景类
 */
class GY_CONTROLLIBRARY_EXPORT GY_DiagramScene : public QGraphicsScene
{
    Q_OBJECT

public:
    enum Mode { InsertItem, InsertLine, InsertText, MoveItem };


    explicit GY_DiagramScene(QMenu *itemMenu, QObject *parent = nullptr);
    QFont font() const { return myFont; }
    QColor textColor() const { return myTextColor; }
    QColor itemColor() const { return myItemColor; }
    QColor lineColor() const { return myLineColor; }

    void setLineColor(const QColor &color);  //
    void setTextColor(const QColor &color);  //设置文本图元字体颜色
    void setItemColor(const QColor &color);  //设置图元背景颜色
    void setItemTextColor(const QColor &color);  //设置图元文字颜色
    void setItemFontPixelSize(int value);  //设置图元字体像素大小
    void setFont(const QFont &font);  //设置文本图元字体
    Mode getMode()const {return myMode;}
    void mousePress(QGraphicsSceneMouseEvent *mouseEvent);
    void setLineType(GY_Arrow::ArrowType);
    void setPort(GY_PortItem* port){mPort = port;}
    void setAddItemAtPoint(QPointF pt){addItemAtPoint=pt;}
    QGraphicsItem* SelectedItem(QPointF);
    qreal GetScale() const {return mScale;}
    qreal GetMiniScale() const {return miniScale;}
    void SaveScale(qreal scale) { mScale = scale;}
    void SetSceneScale(int zoomIO);
    void SaveScene(QString group, QString name, QJsonObject& schemeFile, QString filePath);  //保存方案
    /**
     * @brief SavePython  保存python文件
     * @param scheme  方案
     * @param sigVars  信号变量列表, 格式:{"signal":"","variant":""}，使用此参数则不使用varNames
     * @param varNames  python函数变量名列表，使用此参数则不使用sigVars
     * @param fileName  保存带路径的.py文件名
     */
    static void SavePython(QJsonObject scheme, QJsonArray sigVars, QStringList varNames, QString fileName);
    static void FindItemChain(QJsonArray items, QJsonArray lines, QJsonObject fromObj, QStringList& pyLines
                       , QMap<QString, QMap<QString, QString>>& rets  //局部返回值  Map格式: QMap<item uuid, QMap<ret name, ret value>>
                       , int& indentTimes);
    static void FindItem(QJsonArray items, QJsonArray lines,QJsonObject fromObj, QStringList& pyLines
                       , QMap<QString, QMap<QString, QString>>& rets
                       , QJsonObject line, int& indentTimes);
    GY_Arrow *getSelLine() const;

    void setSelLine(GY_Arrow *value);

public slots:
    void setMode(Mode mode);
    void setItemType(GY_DiagramItem::DiagramType type);
    void editorLostFocus(GY_DiagramTextItem *item);
    void SlotInsertItem(QString,GY_DiagramScene::Mode);
signals:
    void itemInserted(GY_DiagramItem *item);
    void textInserted(QGraphicsTextItem *item);
    void itemSelected(QGraphicsItem *item);
    void sigScale(int delta);
    void sigPopupMenu(QPointF);
    void sigHidePopupMenu();
protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) override;
    void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) override;
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) override;
    void wheelEvent(QGraphicsSceneWheelEvent *) override;
    void keyPressEvent(QKeyEvent *event) override;  //被view event屏蔽
    void keyReleaseEvent(QKeyEvent *event) override;  //被view event屏蔽
    void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;  //未响应
private:
    bool isItemChange(int type) const;

    GY_DiagramItem::DiagramType myItemType;
    QMenu *myItemMenu;
    Mode myMode;
    bool leftButtonDown;
    GY_Arrow* line;  //当前用于绘制连线的线条
    GY_Arrow* selLine = nullptr;  //当前选中的线条
    QPointF startPoint;
    QFont myFont;
    GY_DiagramTextItem *textItem;
    QColor myTextColor;
    QColor myItemColor;  //图元背景颜色
    QColor myItemTextColor;  //图元文字颜色
    int myItemFontPixelSize;  //图元字体像素大小
    QColor myLineColor;
    GY_Arrow::ArrowType lineType;
    GY_PortItem* mPort;  //当前连线的槽，可能是出口槽也可能是入口槽

    QPointF addItemAtPoint;
    qreal mScale;
    const qreal miniScale = 0.5;  //缩小的最小限制值

};


#endif // GY_DIAGRAMSCENE_H
